home *** CD-ROM | disk | FTP | other *** search
- Path: news.mcs.net!not-for-mail
- From: supercat@MCS.COM (John Payson)
- Newsgroups: comp.arch.embedded,comp.lang.c
- Subject: Re: Using malloc of C on embedded system?
- Date: 3 Apr 1996 02:10:05 -0600
- Organization: /usr/lib/news/organi[sz]ation
- Message-ID: <4jtbot$o1@Mars.mcs.com>
- References: <4jml7h$cbc@castor.usc.edu> <4jrndq$ate@kuikka.inet.fi>
- NNTP-Posting-Host: mars.mcs.com
-
- In article <4jrndq$ate@kuikka.inet.fi>,
- Risto Jokinen <hatrjo@hat-fi.kone.com> wrote:
- >This mechanism works. random size malloc/free does not work on embedded systems, or
- >at least it is impossible to debug, and can be VERY slow. fixed size partition pool
- >manager takes <<50us for malloc/free in any processor.
-
- Fixed-size malloc/free is often a good approach if it works; to help it
- along, however, it's often useful to have a "version" of malloc for odd-
- sized blocks that will never be freed (allocate a large memory pool for
- these things and just track the last byte used), and sometimes useful to
- have several memory pools for different-sized objects.
-
- As I mentioned in another post, the "binary buddy" system is often a good
- compromise between internal and external fragmentation in embedded systems.
- All objects consist of a power-of-two number number of "atoms" [smallest
- allocatable chunk] and, for simplicity, the "total" number of atoms must
- be a power of two [if the memory size isn't a power of two, "pre-allocate"
- the memory that isn't there].
-
- If the memory size is N [N being a power of 2] atoms, the memory will then
- consist of N clusters of size 1, which may be paired as N/2 clusters of
- size 2, which may be paired as N/4 clusters of size 4, etc. up to 2 clusters
- of size N/2 and 1 cluster of size N. Each cluster has a pair of bit varia-
- bles which indicate what size clusters could be "perfect-fit" allocated
- within them.
-
- To allocate a cluster of size K, the system looks in both halves of the root
- cluster to see if either half has a perfect match [i.e. somewhere within
- that branch is a cluster of size K*2 which is already half-allocated]. If
- so, it checks the recurses on that branch. If not, it checks to see if either
- branch has a cluster of size K*4 which is partially allocated but has space.
- If not, it checks for size K*8, etc. Finally, it just checks to see if either
- branch has space.
-
- Once the system has recursed to a cluster of size K*2 from which it will
- allocate the new block, it will then clear the "block available" bits from
- the parent clusters as appropriate (in particular, as it traverses up the
- tree, it will clear the "block available bit" for the block's size unless
- or until it finds a block's sibling which has that bit set.
-
- The system is somewhat confusing to describe, but it offers lg(N) performance
- without too much code or--if power-of-two memory sizes work well for your
- application--memory overhead. It also tends to produce minimal fragmentation:
- while isolated unallocated spaces may get formed when non-consecutive blocks
- are freed, such spaces will have effective priority when allocating new blocks.
-
- --
- -------------------------------------------------------------------------------
- supercat@mcs.com | "Je crois que je ne vais jamais voir... | J\_/L
- John Payson | Un animal aussi beau qu'un chat." | ( o o )
-